import { getNovelChapter } from '@/api'

const state = {
  html: '',
  chapterList: [],
  loading: false,
  options: {
    bgColor: '#E9FAFF'
  }
}

const mutations = {
  setNovelChapter (state, data) {
    state.chapterList = data.chapterList
    state.html = data.html
    state.loading = false
  },
  setLoading (state, loading) {
    state.loading = false
  },
  setOptions (state, options) {
    state.options = { ...state.options, ...options }
    localStorage.setItem('chapter-options', state.options)
  }
}

const actions = {
  fetchNovelChapter ({ commit }, params) {
    commit('setLoading', true)

    getNovelChapter(params).then(res => {
      commit('setNovelChapter', res)
    }).catch(e => {
      this.$message.error(e.message)
    })
  }
}

export default {
  state,
  mutations,
  actions
}
